
package org.jhotdraw.samples.phototemplate;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JViewport;

/**
 *
 * @author Eliazar
 */


public class ThumbnailDialog extends javax.swing.JDialog {
    private ButtonGroup group;
    private Map<JRadioButton, URI> mapRadioURI;
    private Map<URI, JLabel> mapThumbnailsOnDropZone; 
    private URI editedFigure;
    private URI selectedThumbnail;
    private URI[] selectedURIs;
    private final int sizeThumbnail = 70;
    
    public ThumbnailDialog(java.awt.Frame parent, boolean modal, final Map<URI, JLabel> mapThumbnails) {
        super(parent, modal);
        initComponents();
        btEdit.setEnabled(false);
        btAddToTemplate.setEnabled(false);        
        this.mapThumbnailsOnDropZone = mapThumbnails;        
        mapRadioURI = new HashMap<JRadioButton, URI>();        
        createPanelThumnbnails();
        pack();
    }
   
    public URI getEditedFigure() {
        return editedFigure;
    }

    public URI[] getSelectedURIs() {
        return selectedURIs;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelGeneral = new javax.swing.JPanel();
        scrollpanelThumbnails = new javax.swing.JScrollPane();
        panelThumbnails = new javax.swing.JPanel();
        btAddToTemplate = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Selecionar Imagens");
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                releaseResources(evt);
            }
        });

        panelGeneral.setBorder(javax.swing.BorderFactory.createTitledBorder("Selecione uma das imagens"));
        panelGeneral.setLayout(null);

        javax.swing.GroupLayout panelThumbnailsLayout = new javax.swing.GroupLayout(panelThumbnails);
        panelThumbnails.setLayout(panelThumbnailsLayout);
        panelThumbnailsLayout.setHorizontalGroup(
            panelThumbnailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 628, Short.MAX_VALUE)
        );
        panelThumbnailsLayout.setVerticalGroup(
            panelThumbnailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 168, Short.MAX_VALUE)
        );

        scrollpanelThumbnails.setViewportView(panelThumbnails);

        panelGeneral.add(scrollpanelThumbnails);
        scrollpanelThumbnails.setBounds(10, 20, 610, 170);

        btAddToTemplate.setText("Adicionar");
        btAddToTemplate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddToTemplateActionPerformed(evt);
            }
        });
        panelGeneral.add(btAddToTemplate);
        btAddToTemplate.setBounds(430, 200, 100, 23);

        btEdit.setText("Editar");
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });
        panelGeneral.add(btEdit);
        btEdit.setBounds(541, 200, 80, 23);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelGeneral, javax.swing.GroupLayout.PREFERRED_SIZE, 630, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelGeneral, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddToTemplateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddToTemplateActionPerformed
        setSelectedURIs();        
        setVisible(false);               
    }//GEN-LAST:event_btAddToTemplateActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
        EditionImageDialog dialog = new EditionImageDialog(null, true, selectedThumbnail);                        
        dialog.setLocationRelativeTo(null);
        dialog.pack();
        dialog.setVisible(true);
        
        if(dialog.getEditedFigure() != null) {
            editedFigure = dialog.getEditedFigure();
            setSelectedURIs();
            setVisible(false);
        }
        System.gc();
    }//GEN-LAST:event_btEditActionPerformed

    private void setSelectedURIs() {
        selectedURIs = null;
        if(selectedThumbnail != null) {
            selectedURIs = new URI[2];
            selectedURIs[0] = selectedThumbnail;
            if(editedFigure != null) {
                selectedURIs[1] = editedFigure;
            }          
        }
    }
    
    private void releaseResources(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_releaseResources
        System.gc();
    }//GEN-LAST:event_releaseResources
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddToTemplate;
    private javax.swing.JButton btEdit;
    private javax.swing.JPanel panelGeneral;
    private javax.swing.JPanel panelThumbnails;
    private javax.swing.JScrollPane scrollpanelThumbnails;
    // End of variables declaration//GEN-END:variables
        
    private void createPanelThumnbnails() {
        JPanel panel;
        JRadioButton radioButton;        
        JViewport viewport;
        
        // configurando gerenciadores de layout
        panelThumbnails.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        GridBagConstraints gbcPanelTumbnails = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbcPanelTumbnails.insets = new Insets(5, 5, 10, 10);
        gbcPanelTumbnails.gridx = 0;
        gbcPanelTumbnails.gridy = 0;
        
        group = new ButtonGroup();        
        for (URI uri : mapThumbnailsOnDropZone.keySet()) {
            panel = new JPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            panel.setLayout(new GridBagLayout());
            radioButton = new JRadioButton();
            mapRadioURI.put(radioButton, uri);
            radioButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                    
                    selectedThumbnail = mapRadioURI.get(e.getSource());
                    btEdit.setEnabled(true);
                    btAddToTemplate.setEnabled(true);
                }
            });
            group.add(radioButton);
            gbc.gridx = 0;
            gbc.gridy = 0;
            panel.add(mapThumbnailsOnDropZone.get(uri), gbc);
            gbc.gridx = 0;
            gbc.gridy = 1;
            panel.add(radioButton, gbc);            
            
            panelThumbnails.add(panel, gbcPanelTumbnails);
            gbcPanelTumbnails.gridx = gbcPanelTumbnails.gridx + 1;                        
        }                                     
        viewport = new JViewport();
        viewport.setPreferredSize(new Dimension(panelThumbnails.getComponents().length * (sizeThumbnail+30), 0));
        viewport.add(panelThumbnails);
        scrollpanelThumbnails.setViewportView(viewport);
    }
}
